package com.yilin.commons.util;

import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import io.woo.htmltopdf.HtmlToPdf;
import io.woo.htmltopdf.HtmlToPdfObject;
import io.woo.htmltopdf.PdfOrientation;
import io.woo.htmltopdf.PdfPageSize;

import javax.swing.*;
import java.io.FileOutputStream;
import java.util.List;

public class PdfUtil {

    /**
     * pdf生成
     */
    public static boolean html2Pdf(String html, String outPath) {
        return html2Pdf( ArrayUtil.initList(html), outPath, PdfOrientation.PORTRAIT,PdfPageSize.A4);
    }

    public static boolean html2Pdf(List<String> htmlList, String outPath) {
        return html2Pdf( htmlList, outPath, PdfOrientation.PORTRAIT,PdfPageSize.A4);
    }
    public static boolean html2Pdf(List<String> htmlList, String outPath,PdfOrientation orientation) {
        return html2Pdf( htmlList, outPath, orientation,PdfPageSize.A4);
    }
    public static boolean html2Pdf(List<String> htmlList, String outPath, PdfOrientation orientation, PdfPageSize pageSize) {
        HtmlToPdf htmlToPdf = HtmlToPdf.create().pageSize(pageSize).disableSmartShrinking(false);
        htmlToPdf.marginTop("1cm").marginBottom("1cm").marginLeft("1cm").marginRight("1cm");//边距
        htmlToPdf.documentTitle("PDF档案").orientation(orientation).dpi(600).imageDpi(300);
        htmlList.forEach(html -> htmlToPdf.object(HtmlToPdfObject.forHtml(html)));
        return htmlToPdf.convert(outPath);
    }

    /**
     * pdf生成水印
     */
    public static String addWaterMarkRep(String pdfParentPath, String pdfName, String WaterMarkContent) {
        return addWaterMark(pdfParentPath + "/" + pdfName + ".pdf", pdfParentPath + "/" + pdfName + "(水印)" + ".pdf", WaterMarkContent, 12);
    }

    public static String addWaterMark(String inputFile, String outputFile, String WaterMarkContent) {
        return addWaterMark(inputFile, outputFile, WaterMarkContent, 12);
    }

    public static String addWaterMark(String inputFile, String outputFile, String waterMarkName, int numberOfPage) {
        try {
            // 水印的高和宽（参数可调节）
            int textH = 60;
            int textW = 170;
            // 间隔距离（参数可调节）
            int interval = 30;
            // 切记这里的参数是文件的路径 ，路径必须是双斜杠的如F:\\test.pdf，不能是F:/test.pdf 或者F:\test.pdf
            PdfReader reader = new PdfReader(inputFile);
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
            BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
            PdfGState gs = new PdfGState();
            gs.setFillOpacity(0.2f);//改透明度
            gs.setStrokeOpacity(0.4f);
            int total = reader.getNumberOfPages() + 1;
            JLabel label = new JLabel();
            label.setText(waterMarkName);
            PdfContentByte under;
            // 可添加多个水印
            Rectangle pageRect = null;
            label.setText(waterMarkName);
            for (int i = 1; i < total; i++) {
                pageRect = reader.getPageSizeWithRotation(i);
                // 在内容上方加水印
                under = stamper.getOverContent(i);
                under.saveState();
                under.setGState(gs);
                under.beginText();
                under.setFontAndSize(base, 16);
                // 水印文字成30度角倾斜
                for (int height = interval + textH; height < pageRect.getHeight(); height = height + textH * 3) {
                    for (int width = interval + textW; width < pageRect.getWidth() + textW;
                         width = width + textW * 2) {
                        under.showTextAligned(Element.ALIGN_LEFT, waterMarkName, width - textW, height - textH, 30);
                    }
                }
                // 添加水印文字
                under.endText();
            }
            stamper.close();
            reader.close();
            return outputFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
